package com.yaozheng.vocationaltraining.service.impl;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IBaseView;
import com.yaozheng.vocationaltraining.iview.IHomeView;
import com.yaozheng.vocationaltraining.service.HomeService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

/**
 * 主页服务
 */
@EBean
public class HomeServiceImpl implements HomeService {

    IHomeView iHomeView;

    @Override
    public void init(IHomeView iHomeView) {
        this.iHomeView = iHomeView;
    }

    @Background
    @Override
    public void loadHomeData() {
        try {
            OkHttpClientManager.getInstance().get(
                    Constants.ROOTURL + "/system/home",
                    iHomeView.getToken(),
                    new OkHttpClientManager.IBaseViewCallback(new BaseView(iHomeView) {

                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iHomeView.loadHomeDataSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iHomeView.loadHomeDataError(error);
                        }

                    }));
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iHomeView.isResponseResult()) {
                iHomeView
                        .loadHomeDataError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }
}
